#include <stdio.h>
#include <string.h>

char *
simple_memmove (char *dst, const char *src, size_t n)
{
	char *ret = dst;
	if (src < dst)
	{
		dst += n;
		src += n;
		while (n--)
			*--dst = *--src;
	}
	else
		while (n--)
			*dst++ = *src++;
		return ret;
}

void * _memmove
(
 void *	 destination,	/* destination of copy */
 const void * source,	/* source of copy */
 size_t 	 size		/* size of memory to copy */
 )
{
	char *	dest;
	const char *src;
	
	dest = destination;
	src = source;
	
	if ((src < dest) && (dest < (src + size)))
	{
		for (dest += size, src += size; size > 0; --size)
			*--dest = *--src;
        }
	else 
	{
		while (size > 0)
		{
			size--;
			*dest++ = *src++;
		}
        }
	return (destination);
}


static char string1[60] = "The quick brown dog jumps over the lazy fox";
static char string2[60] = "The quick brown fox jumps over the lazy dog";
/*                           1         2         3         4         5
 *                  12345678901234567890123456789012345678901234567890
 */

static void _main( void )
{
	printf( "Function:\tmemcpy without overlap\n" );
	printf( "Source:\t\t%s\n", string1 + 40 );
	printf( "Destination:\t%s\n", string1 + 16 );
	memcpy( string1 + 16, string1 + 40, 3 );
	printf( "Result:\t\t%s\n", string1 );
	printf( "Length:\t\t%d characters\n\n", strlen( string1 ) );
	
	/* Restore string1 to original contents */
	memcpy( string1 + 16, string2 + 40, 3 );
	
	printf( "Function:\tmemmove with overlap\n" );
	printf( "Source:\t\t%s\n", string2 + 4 );
	printf( "Destination:\t%s\n", string2 + 10 );
	simple_memmove( string2 + 10, string2 + 4, 40 );
	printf( "Result:\t\t%s\n", string2 );
	printf( "Length:\t\t%d characters\n\n", strlen( string2 ) );
	
	printf( "Function:\tmemcpy with overlap\n" );
	printf( "Source:\t\t%s\n", string1 + 4 );
	printf( "Destination:\t%s\n", string1 + 10 );
	memcpy( string1 + 10, string1 + 4, 40 );
	printf( "Result:\t\t%s\n", string1 );
	printf( "Length:\t\t%d characters\n\n", strlen( string1 ) );
}